
import { readdirSync, unlinkSync, rmdirSync, statSync, existsSync } from 'fs';
import { resolve } from 'path'
function deepFind(path: string) {
  let files = readdirSync(path) || [];
  let index = 0;
  for (let i = 0; i < files.length; i++) {
    let filePath = resolve(path, files[i]);
    const stat = statSync(filePath);
    if (stat.isFile()) {
      unlinkSync(filePath);
      index += 1;
    } else if (stat.isDirectory()) {
      let bool = rmDir(filePath);
      if (bool) {
        rmdirSync(path);
        index += 1
      }
    }
  }
  return files.length === index;
}
export function rmDir(path: string) {
  if (!existsSync(path)) return true;
  const stat = statSync(path);
  if (stat.isFile()) { return unlinkSync(path) }
  if (stat.isDirectory()) {
    if (deepFind(path)) return rmdirSync(path);
  }
  return false;
}